//
//  LYDownLoadViewController.m
//  MusicMV
//
//  Created by dllo on 16/3/31.
//  Copyright © 2016年 Eternity. All rights reserved.
//

#import "LYDownLoadViewController.h"

@interface LYDownLoadViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;
@end

@implementation LYDownLoadViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
   self.navigationItem.title = @"我的下载音乐";
    
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    [self createView];
    
}

- (void)viewWillAppear:(BOOL)animated {
    
    NSMutableArray *arr = [self getArr];
    if (arr.count == 0) {
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, WIDTH, 100)];
        [self.view addSubview:label];
        label.text = @"对不起，您还有没有下载哟";
        
        
    }
    
    
}

// 从本地获取下载的数组
- (NSMutableArray *)getArr {
    
    NSArray *pathArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentPath = [pathArr lastObject];
    NSString *arrPath = [documentPath stringByAppendingPathComponent:@"dic.list"];
    
    NSArray *arr = [NSArray arrayWithContentsOfFile:arrPath];
    NSMutableArray *arrM = [NSMutableArray arrayWithArray:arr];
    
    return arrM;
    
}

- (void)createView {
    
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    
    [_tableView registerClass:[LYDownLoadTableViewCell class] forCellReuseIdentifier:@"tableView"];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    NSMutableArray *arr = [self getArr];
    return arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    LYDownLoadTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableView"];
    
    NSArray *arr = [self getArr];
    NSMutableArray *array = [LYDayMusicModel modelWithArray:arr];
    
    LYDayMusicModel *model = array[indexPath.row];
    cell.model = model;
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 70 * ht;
}


- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:animated];
    
    
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
    
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        NSFileManager *manager = [NSFileManager defaultManager];
        
        NSMutableArray *arrayModel = [self getArr];
        // 先创建一个路径 然后删掉
        NSString *catchPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)  lastObject] stringByAppendingString:[NSString stringWithFormat:@"/HSCache/Download/Music/%lu.mp4",(unsigned long)[arrayModel[indexPath.row][@"music"] hash]]];
        [manager removeItemAtPath:catchPath error:nil];
        [arrayModel removeObjectAtIndex:indexPath.row];
        
        NSArray *pathArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSLog(@"%@", pathArr);
        NSString *documentPath = [pathArr lastObject];
        NSString *arrPath = [documentPath stringByAppendingPathComponent:@"dic.list"];
        
        [arrayModel writeToFile:arrPath atomically:YES];
        
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [_tableView reloadData];
        
        if (arrayModel.count == 0) {
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, WIDTH, 100)];
            [self.view addSubview:label];
            label.text = @"对不起，您还没有下载音乐哟";
            _tableView.alpha = 0;
        }

    }
    
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    
//    LYMusicPlayerViewController *music = [[LYMusicPlayerViewController alloc] init];
//    
//    music.musicArr = [self getArr];
//    
//    
//    
//    [self presentViewController:music animated:YES completion:^{
//        
//    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
